package cn.iocoder.yudao.module.paymember.mq.consumer.user;

import cn.iocoder.yudao.module.member.service.usercommission.UserCommissionService;
import cn.iocoder.yudao.module.pay.dal.dataobject.packageorder.PackageOrderDO;
import cn.iocoder.yudao.module.pay.mq.message.packageorder.PayPackageOrderSuccessMessage;
import cn.iocoder.yudao.module.pay.service.packageorder.PackageOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 套餐订单支付成功，计算用户佣金的消费者
 */
@Slf4j
@Component
public class UserCommissionPayPackageOrderSuccessConsumer {

    @Resource
    private PackageOrderService packageOrderService;
    @Resource
    private UserCommissionService userCommissionService;

    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    @EventListener
    public void onMessage(PayPackageOrderSuccessMessage message) {
        log.info("[onMessage][消息内容({})]", message);
        String merchantOrderId = message.getMerchantOrderId();
        PackageOrderDO packageOrderDO = packageOrderService.getPackageOrder(Long.parseLong(merchantOrderId));
        userCommissionService.addUserCommission(packageOrderDO.getUserId(), packageOrderDO.getId(), packageOrderDO.getPrice());
    }

}
